package com.xiamaliu;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        // 创建原型对象
        ConcretePrototype prototype = new ConcretePrototype();
        prototype.setAge(10);
        prototype.setName("李三");
        System.out.println(prototype);
        // 拷贝原型对象
        ConcretePrototype clone = prototype.clone();
        System.out.println(clone);


        // 浅拷贝
        ConcretePrototype2 prototype2 = new ConcretePrototype2();
        prototype2.setAge(10);
        prototype2.setName("李三");
        List<String> hobbies = new ArrayList<>();
        hobbies.add("打球");
        hobbies.add("打豆豆");
        prototype2.setHobbies(hobbies);
        System.out.println(prototype2);
        // 拷贝原型对象（浅拷贝）
        ConcretePrototype2 clone2 = prototype2.clone();
        clone2.getHobbies().add("吃饭");
        System.out.println("浅拷贝原型对象：" + prototype2);
        System.out.println("浅拷贝克隆对象：" + clone2);
        System.out.println(prototype2 == clone2);


        // 深拷贝
        ConcretePrototype3 prototype3 = new ConcretePrototype3();
        prototype3.setAge(10);
        prototype3.setName("李三");
        List<String> hobbies2 = new ArrayList<>();
        hobbies2.add("打球");
        hobbies2.add("打豆豆");
        prototype3.setHobbies(hobbies2);
        System.out.println(prototype3);
        // 拷贝原型对象（深拷贝）
        ConcretePrototype3 clone3 = prototype3.deepClone();
        clone3.getHobbies().add("吃饭");
        System.out.println("深拷贝原型对象：" + prototype3);
        System.out.println("深拷贝克隆对象：" + clone3);
        System.out.println(prototype3 == clone3);
        System.out.println("深拷贝原型对象的爱好：" + prototype3.getHobbies());
        System.out.println("深拷贝克隆对象的爱好：" + clone3.getHobbies());
        System.out.println(prototype3.getHobbies() == clone3.getHobbies());

        // 使用ArrayList重写List克隆
        ConcretePrototype3 clone4 = prototype3.deepCloneHobbies();
        clone4.getHobbies().add("打游戏");
        System.out.println("深拷贝原型对象：" + prototype3);
        System.out.println("深拷贝克隆对象：" + clone4);
        System.out.println(prototype3 == clone4);
        System.out.println("深拷贝原型对象的爱好：" + prototype3.getHobbies());
        System.out.println("深拷贝克隆对象的爱好：" + clone4.getHobbies());
        System.out.println(prototype3.getHobbies() == clone4.getHobbies());
    }
}
